<?php
/*
 * 品牌
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Brand;


class BrandController extends BaseController{

    //品牌列表
    public function brandList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $brand_name = Request::param('brand_name');
//        $type = Request::param('type');

        $where = [];
        $where[] = ['enable','=','1'];
        if(!empty($w_name)){
            $where[] = ['brand_name','like','%'.$brand_name.'%'];
        }
//        if(!empty($type)){
//            $where[] = ['type','=',$type];
//        }
        $list = Brand::brandList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Brand::brandCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //品牌添加
    public function brandAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $brand_name = Request::param('brand_name');
        $brand_img = Request::param('brand_img');
//        $type = Request::param('type');
//        $province = Request::param('province');
//        $city = Request::param('city');
//        $district = Request::param('district');
//        $address = Request::param('address');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'brand_name' => $brand_name,
            'enable' => $enable,
            'addtime' => $time,
            'brand_img' => $brand_img,
//            'province' => $province,
//            'city' => $city,
//            'address' => $address,
//            'district' => $district,
//            'address' => $address,

        ];

        Brand::brandAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //品牌详情
    public function brandInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Brand::brandInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //品牌修改
    public function brandEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $brand_name = Request::param('brand_name');
        $brand_img = Request::param('brand_img');
        
        $update = [];
        if(!empty($brand_name)){
            $update['brand_name'] = $brand_name;
        }
        
        if(!empty($brand_img)){
            $update['brand_img'] = $brand_img;
        }
        
//        if(!empty($province)){
//            $update['province'] = $province;
//        }
//        
//        if(!empty($city)){
//            $update['city'] = $city;
//        }
//        
//        if(!empty($district)){
//            $update['district'] = $district;
//        }
//        
//        if(!empty($address)){
//            $update['address'] = $address;
//        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Brand::brandEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
